﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Crawler.Model;
using Husb.Framework.DataAccess;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Crawler.DataAccess
{
    public partial class TaskDao : DataAccessBase<TaskInfo>
    {
        public TaskDao()
        {
            //ObjectId = "Id";
            //MasterObjectId = "";
            //DetailObjectId = "";
            CommandType = System.Data.CommandType.StoredProcedure;

            GetByIdStatement = "Crawler.Tasks_SelectTask";
            //GetByNameStoredProc = SR.SelectWebPageByName;
            FindAllStatement = "Crawler.Tasks_SelectTasksAll";
            //FindStoredProc = SR.SelectWebPagesDynamic;
            //FindByNameStoredProc = SR.SelectWebPagesByNameInexact;


            InsertStatement = "Crawler.Tasks_InsertTask";
            UpdateStatement = "Crawler.Tasks_UpdateTask";
            //CreateUpdateStoredProc = SR.InsertUpdateWebPage;
            DeleteByIdStatement = "Crawler.Tasks_DeleteTask";
            //DeleteByObjectStoredProc = SR.DeleteWebPage;

            //GetRowCountStoredProc = SR.SelectWebPagesRowCount;
            //GetPagedSortedProc = SR.SelectWebPagesPaged;
        }

        #region PopulateParamters
        /// <summary>
        /// 根据实体类的属性，给数据库命令的参数赋值
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="cmd">命令对象</param>
        /// <param name="task">任务</param>
        protected override void PopulateParamters(TaskInfo task, Database db, System.Data.Common.DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, task.Id);
            db.AddInParameter(cmd, "Name", DbType.String, task.Name);
            db.AddInParameter(cmd, "CategoryId", DbType.Guid, task.CategoryId);
            db.AddInParameter(cmd, "TaskType", DbType.Int32, task.TaskType);
            db.AddInParameter(cmd, "Encoding", DbType.AnsiString, task.Encoding);
            db.AddInParameter(cmd, "Cookie", DbType.AnsiString, task.Cookie);
            db.AddInParameter(cmd, "LoginUrl", DbType.AnsiString, task.LoginUrl);
            db.AddInParameter(cmd, "Executed", DbType.Boolean, task.Executed);
            db.AddInParameter(cmd, "Version", DbType.Int32, task.Version);
        }
        #endregion

        #region  LoadTaskInfo
        /// <summary>
        /// 根据数据库的查询结果，给实体类的属性赋值
        /// </summary>
        /// <param name="task">任务</param>
        /// <param name="dr"><see cref="IDataReader" />类的一个实例</param>
        protected override void LoadProperty(TaskInfo task, System.Data.IDataReader dr)
        {
            task.Id = (System.Guid)dr["Id"];
            task.Name = (System.String)dr["Name"];
            task.CategoryId = dr["CategoryId"] == DBNull.Value ? Guid.Empty : (System.Guid?)dr["CategoryId"];
            task.TaskType = dr["TaskType"] == DBNull.Value ? (int)0 : (System.Int32?)dr["TaskType"];
            task.Encoding = dr["Encoding"] == DBNull.Value ? "" : (System.String)dr["Encoding"];
            task.Cookie = dr["Cookie"] == DBNull.Value ? "" : (System.String)dr["Cookie"];
            task.LoginUrl = dr["LoginUrl"] == DBNull.Value ? "" : (System.String)dr["LoginUrl"];
            task.Executed = dr["Executed"] == DBNull.Value ? false : (System.Boolean?)dr["Executed"];
        }
        #endregion

        #region LoadHalfTaskInfo
        /// <summary>
        /// 根据执行不同的数据获取命令，得到一个不完整的TaskInfo<see cref="TaskInfo" />对象，也许只有部分属性有内容。
        /// </summary>
        /// <param name="task"></param>
        /// <param name="dr">IDataReader<see cref="IDataReader" /></param>
        public static void LoadHalfTaskInfo(TaskInfo task, System.Data.IDataReader dr)
        {
            for (int fieldCount = 0; fieldCount < dr.FieldCount; fieldCount++)
            {
                switch (dr.GetName(fieldCount))
                {
                    case "Id":
                        task.Id = (System.Guid)dr["Id"];
                        break;
                    case "Name":
                        task.Name = (System.String)dr["Name"];
                        break;
                    case "CategoryId":
                        task.CategoryId = dr["CategoryId"] == DBNull.Value ? Guid.Empty : (System.Guid?)dr["CategoryId"];
                        break;
                    case "TaskType":
                        task.TaskType = dr["TaskType"] == DBNull.Value ? (int)0 : (System.Int32?)dr["TaskType"];
                        break;
                    case "Encoding":
                        task.Encoding = dr["Encoding"] == DBNull.Value ? "" : (System.String)dr["Encoding"];
                        break;
                    case "Cookie":
                        task.Cookie = dr["Cookie"] == DBNull.Value ? "" : (System.String)dr["Cookie"];
                        break;
                    case "LoginUrl":
                        task.LoginUrl = dr["LoginUrl"] == DBNull.Value ? "" : (System.String)dr["LoginUrl"];
                        break;
                    case "Executed":
                        task.Executed = dr["Executed"] == DBNull.Value ? false : (System.Boolean?)dr["Executed"];
                        break;
                }
            }
        }
        #endregion
    }
}
